<?php
/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Flashgame
 */
?>

<script type="text/javascript">
  var pageAction =function(page){
    $('page').value = page;
    $('filter_form').submit();
  }

  en4.core.runonce.add(function(){
    $$('#filter_form input[type=text]').each(function(f) {
        if (f.value == '' && f.id.match(/\min$/)) {
            new OverText(f, {'textOverride':'min','element':'span'});
            //f.set('class', 'integer_field_unselected');
        }
        if (f.value == '' && f.id.match(/\max$/)) {
            new OverText(f, {'textOverride':'max','element':'span'});
            //f.set('class', 'integer_field_unselected');
        }
    });
  });
</script>

<div class="headline">
  <h2>
    <?php echo $this->translate('Games');?>
  </h2>
  <div class="tabs">
    <?php
      // Render the menu
      echo $this->navigation()
        ->menu()
        ->setContainer($this->navigation)
        ->render();
    ?>
  </div>
</div>

<div class='layout_right'>
  <?php echo $this->form->render($this) ?>
  <?php if( $this->can_create): ?>
  <div class="quicklinks">
    <ul>
      <li>
        <a href='<?php echo $this->url(array(), 'flashgame_create', true) ?>' class='buttonlink icon_flashgame_new'><?php echo $this->translate('Post New Game');?></a>
      </li>
    </ul>
  </div>
  <?php endif; ?>
</div>

<div class='layout_middle'>
  <?php if ($this->current_count >= $this->quota):?>
    <div class="tip">
      <span>
        <?php echo $this->translate('You have already created the maximum number of games allowed. If you would like to create a new game, please delete an old one first.');?>
      </span>
    </div>
    <br/>
  <?php endif; ?>
  <?php if( $this->paginator->getTotalItemCount() > 0 ): ?>
    <ul class="flashgames_browse">
      <?php foreach( $this->paginator as $item ): ?>
        <li>
          <div class='flashgames_browse_options'>
            <?php if ($this->can_edit): ?><a href='<?php echo $this->url(array('flashgame_id' => $item->flashgame_id), 'flashgame_edit', true) ?>' class='buttonlink icon_flashgame_edit'><?php echo $this->translate('Edit Game');?></a><?php endif; ?>
            <?php if ($this->can_delete): ?><a href='<?php echo $this->url(array('flashgame_id' => $item->flashgame_id), 'flashgame_delete', true) ?>' class='buttonlink icon_flashgame_delete'><?php echo $this->translate('Delete Game');?></a><?php endif; ?>
          </div>
          <div class='flashgames_browse_info'>
            <div class='flashgames_browse_info_title'>
              <div class='flashgames_browse_photo'>
			  <?php
				if ($item->photo_id) echo $this->htmlLink($item->getHref(), $this->itemPhoto($item, 'thumb.normal'));
				else echo $this->htmlLink($item->getHref(),'<img alt="" src="application/modules/Flashgame/externals/images/nophoto.png">');
			  ?>
			  </div>
			  <h3>
			      <?php 
                        if (strlen($item->getTitle())>20) 
                            $gtitle = substr(strip_tags($item->getTitle()),0,20)."...";
                        else
                            $gtitle = substr(strip_tags($item->getTitle()),0,20);
                  ?>
                <?php echo $this->htmlLink($item->getHref(), $gtitle) ?>
                <?php if( $item->closed ): ?>
                  <img alt="close" src='application/modules/Flashgame/externals/images/close.png'/>
                <?php endif;?>
              </h3>
            </div>
            <div class='flashgames_browse_info_date'>
              <?php echo $this->timestamp(strtotime($item->creation_date)) ?>
              -
              <?php echo $this->translate('posted by');?> <?php echo $this->htmlLink($item->getOwner()->getHref(), $item->getOwner()->getTitle()) ?>
              <div>
                <?php for($i = 1; $i <= 5; $i++): ?>
                  <img border="0" src="application/modules/Flashgame/externals/images/<?php if ($i <= $item->rates): ?>star_full.png<?php elseif( $i > $item->rates &&  ($i-1) <  $item->rates): ?>star_part.png<?php else: ?>star_none.png<?php endif; ?>" width="10px" />
                <?php endfor; ?>
              </div>
            </div>
            <div class='flashgames_browse_info_blurb'>
              <?php $fieldStructure = Engine_Api::_()->fields()->getFieldsStructurePartial($item)?>
              <?php echo $this->fieldValueLoop($item, $fieldStructure) ?>
              <?php
                // Not mbstring compat
                echo substr(strip_tags($item->body), 0, 350); if (strlen($item->body)>70) echo "...";
              ?>
            </div>
          </div>
        </li>
      <?php endforeach; ?>
    </ul>

  <?php elseif($this->search): ?>
    <div class="tip">
      <span>
        <?php echo $this->translate('You do not have any game that match your search criteria.');?>
      </span>
    </div>
  <?php else:?>
    <div class="tip">
      <span>
        <?php echo $this->translate('You do not have any game.');?>
        <?php if ($this->can_create): ?>
          <?php echo $this->translate('Get started by <a href=\'%1$s\'>posting</a> a new game.', $this->url(array(), 'flashgame_create'));?>
        <?php endif; ?>
      </span>
    </div>
  <?php endif; ?>
  <?php echo $this->paginationControl($this->paginator, null, array("pagination/pagination.tpl","flashgame")); ?>
</div>
